package com.wu.webflux3.customUserService;

import java.util.ArrayList;
import java.util.Collection;

import javax.annotation.Resource;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.ReactiveUserDetailsService;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import reactor.core.publisher.Mono;

public class CustomReactiveUserDetailsService implements ReactiveUserDetailsService {

	@Resource
	private PasswordEncoder passwordEncoder;

	@Override
	public Mono<UserDetails> findByUsername(String username) {
		Collection<GrantedAuthority> authorities = new ArrayList<>();
		authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
		User user = new User("wu",new BCryptPasswordEncoder().encode("123"),authorities);
		return user == null ? Mono.empty() : Mono.just(User.withUserDetails(user).build());
	}

}
